<template>
	<view class="menu-nav">
		<scroll-view scroll-x @scroll="ScrollMenu" class="nav-list">
			<view class="nav" ref="nav">
				<view class="list" v-for="(item,index) in resdata" @click="onSkip('menu')" :key="item.id">
					<image :src="item.src" mode=""></image>
					<text>{{item.text}}</text>
				</view>
			</view>
		</scroll-view>
		<view class="indicator" v-if="resdata.length>10">
			<view class="plan">
				<view class="bar" :style="'left:'+slideNum+'%'"></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//导航条
				slideNum: 0,
			};
		},
		props: {
			resdata: [Array, Object],
		},
		methods: {
			/**
			 * 菜单导航滚动
			 */
			ScrollMenu(e) {
				let scrollLeft = e.target.scrollLeft;
				const query = uni.createSelectorQuery().in(this);
				query.select('.nav').boundingClientRect(data => {
					//滚动的主视图系数 = 总宽度-可视宽度-滚动的距离*2
					let wid = e.target.scrollWidth - data.width - (data.left * 2 + 5);
					//导航条 = 页面偏移的距离/滚动的主视图系数*300(分成三份)/2(px转化为rpx)
					this.slideNum = (scrollLeft / wid * 300) / 2;
				}).exec();
			},
			/**
			 * 菜单导航跳转
			 */
			onSkip(type) {
				uni.navigateTo({
					url: '/pages/SearchGoodsList/SearchGoodsList'
				})
			}
		}
	}
</script>

<style lang="scss">
	/* 菜单导航 */
	.menu-nav {
		position: relative;
		width: 100%;
		height: 300rpx;
		margin: 30rpx auto;

		.nav-list {
			white-space: nowrap;
			height: 270rpx;
			width: 100%;

			.nav {
				display: inline-block;
				display: flex;
				flex-direction: column;
				flex-wrap: wrap;
				justify-content: space-between;
				height: 300rpx;
			}

			.list {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 20%;
				height: 130rpx;
				margin-bottom: 20rpx;

				image {
					width: 75rpx;
					height: 75rpx;
					border-radius: 100%;
				}

				text {
					font-size: 26rpx;
					color: #363636;
					margin-top: 10rpx;
				}
			}
		}

		.indicator {
			position: absolute;
			left: 0;
			bottom: 0;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100%;
			height: 30rpx;

			.plan {
				position: relative;
				width: 100rpx;
				height: 8rpx;
				border-radius: 8rpx;
				background-color: #e1e1e1;

				.bar {
					position: absolute;
					width: 50%;
					height: 100%;
					border-radius: 6rpx;
					background-color: $base;
				}
			}
		}
	}
</style>
